package net.gyapp.markim.module.mine.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.WrapperApplication;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.mine.vo.MemberVo;
import net.gyapp.markim.utils.RequestParams;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class BindMobileActivity extends WrapperSwipeActivity<CommonPresenter> {

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    String mobile;

    private boolean isGain;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what <= 0) {
                isGain = false;
                tvGetCode.setText("发送验证码");
                handler.removeCallbacksAndMessages(null);
            } else {
                isGain = true;
                tvGetCode.setText(String.format("重新发送(%1$s)", msg.what));
                handler.sendEmptyMessageDelayed(--msg.what, 1000);
            }
        }
    };

    public static Intent getIntent(Context context) {
        return new Intent(context, BindMobileActivity.class);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_bind_mobile;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("绑定手机号");
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_GET_CODE)) {
            showToast("验证码已发送");
            handler.sendEmptyMessage(59);
        } else if (url.contains(ApiConfig.API_BIND_PHONE)) {
            showToast("绑定成功");
            //更新会员信息
            MemberVo member = WrapperApplication.getMember();
            member.userInfo.phone = mobile;
            WrapperApplication.setMember(member);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }


    @OnClick({R.id.tv_get_code, R.id.btn_confirm})
    public void onViewClicked(View view) {
        mobile = etMobile.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        switch (view.getId()) {
            case R.id.tv_get_code:
                if (!isGain) {
                    getCode(mobile);
                }
                break;
            case R.id.btn_confirm:
                String code = etCode.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    showToast("请输入验证码");
                    return;
                }
                bind(mobile, code);
                break;
        }
    }

    private void getCode(String mobile) {
        presenter.postData(ApiConfig.API_GET_CODE, new RequestParams().put("phone", mobile).get(), BaseVo.class);
    }

    private void bind(String mobile, String code) {
        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_BIND_PHONE,
                new RequestParams()
                        .put("phone", mobile)
                        .put("code", code)
                        .get(),
                BaseVo.class);
    }
}
